<?php

namespace app\admin\controller\site;

use app\admin\controller\Base;
use app\admin\controller\Qiniu;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Productinfo extends Base
{

    public function index()
    {
        return view();
    }

    public function add()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {
            $validate = new \app\admin\validate\Productinfo();
            if (!$validate->check($post)) {
                $this->error($validate->getError());
            }
            Db::startTrans();
            try {
                $post['status'] = empty($post['status']) ? 2 : 1;
                DB::name('product_info')->insert($post);
                Db::commit();
                $status = 1;
            } catch (\Exception $e) {
                Db::rollback();
                $status = 0;
            }
            if ($status) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');

        } else {
            if (isset($post['id']) && !empty($post['id'])) {
                $info = DB::name('product_info')->where('id', $post['id'])->find();
                $this->assign('info', $info);
            }
            $product = DB::name('product')->select();
            $this->assign('product', $product);
            return view();
        }
    }

    public function delete()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['id'])) {
                $this->error('请求参数错误');
            }
            if (Db::name('product_info')->where(['id' => $post['id']])->update(['status'=>3])) {
                $this->success('操作成功!');
            }
            $this->error('操作失败');
        }
    }

    public function lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        $map[] = ['product_info.status', '<', 3];
        $data['count'] = Db::view('product_info', 'id,title,price,day,status,product_id')
            ->view('product', ['id' => 'product_id', 'title' => 'product_title'], 'product_info.product_id=product.id')
            ->where($map)
            ->count();
        $data['data'] = Db::view('product_info', 'id,title,price,day,status,product_id')
            ->view('product', ['id' => 'product_id', 'title' => 'product_title'], 'product_info.product_id=product.id')
            ->where($map)
            // ->fetchSql(true)
            ->order('id desc')->page(input("param.page"), input('param.limit'))->select();
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();

            if (DB::name('product_info')->update($post) !== false) {
                $this->success('操作成功');
            }
            $this->error('操作失败');
        }
    }

}